// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   Zoom.java

package org.achartengine.tools;

import java.util.*;
import org.achartengine.chart.*;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;

// Referenced classes of package org.achartengine.tools:
//			AbstractTool, ZoomEvent, ZoomListener

public class Zoom extends AbstractTool
{

	private boolean mZoomIn;
	private float mZoomRate;
	private List mZoomListeners;

	public Zoom(AbstractChart abstractchart, boolean flag, float f)
	{
		super(abstractchart);
		mZoomListeners = new ArrayList();
		mZoomIn = flag;
		setZoomRate(f);
	}

	public void setZoomRate(float f)
	{
		mZoomRate = f;
	}

	public void apply()
	{
		if (mChart instanceof XYChart)
		{
			int i = mRenderer.getScalesCount();
			for (int j = 0; j < i; j++)
			{
				double ad[] = getRange(j);
				checkRange(ad, j);
				double ad1[] = mRenderer.getZoomLimits();
				boolean flag = ad1 != null && ad1.length == 4;
				double d = (ad[0] + ad[1]) / 2D;
				double d1 = (ad[2] + ad[3]) / 2D;
				double d2 = ad[1] - ad[0];
				double d3 = ad[3] - ad[2];
				if (mZoomIn)
				{
					if (mRenderer.isZoomXEnabled())
						d2 /= mZoomRate;
					if (mRenderer.isZoomYEnabled())
						d3 /= mZoomRate;
				} else
				{
					if (mRenderer.isZoomXEnabled())
						d2 *= mZoomRate;
					if (mRenderer.isZoomYEnabled())
						d3 *= mZoomRate;
				}
				if (mRenderer.isZoomXEnabled())
				{
					double d4 = d - d2 / 2D;
					double d6 = d + d2 / 2D;
					if (!flag || ad1[0] <= d4 && ad1[1] >= d6)
						setXRange(d4, d6, j);
				}
				if (mRenderer.isZoomYEnabled())
				{
					double d5 = d1 - d3 / 2D;
					double d7 = d1 + d3 / 2D;
					if (!flag || ad1[2] <= d5 && ad1[3] >= d7)
						setYRange(d5, d7, j);
				}
			}

		} else
		{
			DefaultRenderer defaultrenderer = ((RoundChart)mChart).getRenderer();
			if (mZoomIn)
				defaultrenderer.setScale(defaultrenderer.getScale() * mZoomRate);
			else
				defaultrenderer.setScale(defaultrenderer.getScale() / mZoomRate);
		}
		notifyZoomListeners(new ZoomEvent(mZoomIn, mZoomRate));
	}

	private synchronized void notifyZoomListeners(ZoomEvent zoomevent)
	{
		ZoomListener zoomlistener;
		for (Iterator iterator = mZoomListeners.iterator(); iterator.hasNext(); zoomlistener.zoomApplied(zoomevent))
			zoomlistener = (ZoomListener)iterator.next();

	}

	public synchronized void notifyZoomResetListeners()
	{
		ZoomListener zoomlistener;
		for (Iterator iterator = mZoomListeners.iterator(); iterator.hasNext(); zoomlistener.zoomReset())
			zoomlistener = (ZoomListener)iterator.next();

	}

	public synchronized void addZoomListener(ZoomListener zoomlistener)
	{
		mZoomListeners.add(zoomlistener);
	}

	public synchronized void removeZoomListener(ZoomListener zoomlistener)
	{
		mZoomListeners.add(zoomlistener);
	}
}
